/** \file    bmpfill.cpp
  * \brief   routines for filling areas in bitmaps.
  * \date    2002
  * \author  Fedor Chelnokov
  * \version $Header: /home/cscom1r4/cvsroot/cs/imglib/src/bmpfill.cpp,v 1.1 2004/02/04 14:43:23 cscom1r4 Exp $
  */

#include <algorithm>

#include "imglib/bmpfill.h"
#include "imglib/bmpfill_impl.h"
#include "imglib/segmbasic.h"
#include "imglib/imageclass.h"

namespace img {

typedef short_index_map::elem_type	E;
//typedef std::vector<E>			piece_root_vec;

//insert new element in a vector, ordered by operator<
/*inline void insert_if_not(piece_root_vec &vec, E val)
{
	if (!std::binary_search(vec.begin(), vec.end(), val)) {
		vec.push_back(val);
		std::inplace_merge(vec.begin(), vec.end()-1, vec.end());
	}
}*/

void fill_interior(bitmap &bmp)
{
	size_t w = bmp.width(), h = bmp.height();

	//divide frame on pieces
	std::vector< segm_piece<> > piece_vector;
	short_index_map imap(w,h);
	segmbasic4(imap, piece_vector, simple_checker<bitmap>(bmp));

	E root = merge_reset_border(imap, piece_vector, bmp, E(~0));
	if (root == E(~0)) {
		bmp.fill(true);
		return;
	}

	reset_segment(imap, piece_vector, root, bmp);
}

}
